package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.framework.core.result.APIObjResponse;
import com.framework.core.result.BasePage;
import com.project.base.BaseController;
import com.project.biz.CmsProductBiz;
import com.project.biz.CmsProductEvaBiz;
import com.project.biz.CmsUserSearchRecordsBiz;
import com.project.common.model.UserInfo;
import com.project.common.utils.IpageToPage;
import com.project.pojo.dto.ProductDTO;
import com.project.pojo.dto.ProductDetailDTO;
import com.project.pojo.dto.ProductEvaDTO;
import com.project.pojo.dto.ProductSpecAndSkuDTO;
import com.project.pojo.model.*;
import com.project.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 商品
 * @Author: zhuchi
 * @CreateDate: 2020/10/14 19:56
 * @Version: 1.0
 */
@Api(value = "CmsProductController", description = "商品")
@RestController
@RequestMapping("/product")
@Slf4j
public class CmsProductController extends BaseController {

    /**
     * 商品业务层接口类
     */
    @Autowired
    private CmsProductBiz cmsProductBiz;

    /**
     * 商品评价业务层接口类
     */
    @Autowired
    private CmsProductEvaBiz productEvaBiz;

    /**
     * 用户搜索记录业务层接口类
     */
    @Autowired
    private CmsUserSearchRecordsBiz userSearchRecordsBiz;

    /**
     * 后台-查询商品列表
     * @param productModel
     * @return
     */
    @ApiOperation(value = "后台-查询商品列表", httpMethod = "POST")
    @PostMapping(value = "/selectProduct")
    public APIObjResponse<BasePage<ProductDTO>> selectProduct(@RequestBody ProductModel productModel){
        IPage<ProductDTO> iPage = this.cmsProductBiz.selectProduct(productModel);
        BasePage<ProductDTO> basePage = IpageToPage.toBasePage(iPage, ProductDTO.class);
        return APIObjResponse.success("查询成功", basePage);
    }

    /**
     * 添加商品
     * @param addProductModel
     * @return
     */
    @ApiOperation(value = "后台-添加商品", httpMethod = "POST")
    @PostMapping(value = "/addProduct")
    public APIObjResponse<Integer> addProduct(@Validated @RequestBody AddProductModel addProductModel){
        UserInfo userInfo = super.getLoginUser();
        int result = this.cmsProductBiz.addProduct(addProductModel, userInfo.getBaseUserId());
        return APIObjResponse.success("操作成功", result);
    }

    /**
     * 修改商品
     * @param updateProductModel
     * @return
     */
    @ApiOperation(value = "后台-修改商品", httpMethod = "POST")
    @PostMapping(value = "/updateProduct")
    public APIObjResponse<Integer> updateProduct(@Validated @RequestBody UpdateProductModel updateProductModel){
        UserInfo userInfo = super.getLoginUser();
        int result = this.cmsProductBiz.updateProduct(updateProductModel, userInfo.getBaseUserId());
        return APIObjResponse.success("操作成功", result);
    }

    /**
     * 获取商品详情
     * @param productId
     * @return
     */
    @ApiOperation(value = "获取商品详情（前后台通用）", httpMethod = "GET")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ProductDetailDTO.class)})
    @GetMapping(value = "/getProductDetail")
    public APIObjResponse<ProductDetailDTO> getProductDetail(@RequestParam("productId") Long productId){
        ProductDetailDTO productDetailDTO = this.cmsProductBiz.getProductDetail(productId);
        return APIObjResponse.success("查询成功", productDetailDTO);
    }

    /**
     * 删除商品
     * @param productId
     * @return
     */
    @ApiOperation(value = "删除商品", httpMethod = "GET")
    @GetMapping(value = "/delProduct")
    public APIObjResponse<Integer> delProduct(@RequestParam("productId") Long productId){
        UserInfo userInfo = super.getLoginUser();
        int result = this.cmsProductBiz.delProduct(productId, userInfo.getBaseUserId());
        return APIObjResponse.success("操作成功", result);
    }

    /**
     * 商品上下架
     * @param updateProductStateModel
     * @return
     */
    @ApiOperation(value = "商品上下架", httpMethod = "POST")
    @PostMapping(value = "/updateProductState")
    public APIObjResponse<Integer> updateProductState(@Validated @RequestBody UpdateProductStateModel updateProductStateModel){
        UserInfo userInfo = super.getLoginUser();
        int result = this.cmsProductBiz.updateProductState(updateProductStateModel, userInfo.getBaseUserId());
        return APIObjResponse.success("操作成功", result);
    }

    /**
     * 移动端-查询商品列表
     * @param productModel
     * @return
     */
    @ApiOperation(value = "移动端-查询商品列表", httpMethod = "POST")
    @PostMapping(value = "/getProduct")
    public APIObjResponse<BasePage<ProductDTO>> getProduct(@RequestBody ProductModel productModel){
        if (StringUtils.isNotBlank(super.getToken())){
            UserInfo userInfo = super.getLoginUser();
            //添加搜索记录
            if (StringUtils.isNotBlank(productModel.getName())){
                this.userSearchRecordsBiz.addRearchRecords(productModel.getName(), userInfo.getBaseUserId());
            }
        }

        productModel.setState(1);//状态（0:下架,1:上架）
        IPage<ProductDTO> iPage = this.cmsProductBiz.selectProduct(productModel);
        BasePage<ProductDTO> basePage = IpageToPage.toBasePage(iPage, ProductDTO.class);
        return APIObjResponse.success("查询成功", basePage);
    }

    /**
     * 根据商品ID获取商品规格及SKU信息
     * @param productId
     * @return
     */
    @ApiOperation(value = "根据商品ID获取商品规格及SKU信息", httpMethod = "GET")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ProductSpecAndSkuDTO.class)})
    @GetMapping(value = "/getProductSpecAndSku")
    public APIObjResponse<ProductSpecAndSkuDTO> getProductSpecAndSku(@RequestParam("productId") Long productId){
        ProductSpecAndSkuDTO productSpecAndSkuDTO = this.cmsProductBiz.getProductSpecAndSku(productId);
        return APIObjResponse.success("查询成功", productSpecAndSkuDTO);
    }

    /**
     * 分页查询商品评论
     * @param getProductEvaModel
     * @return
     */
    @ApiOperation(value = "分页查询商品评论", httpMethod = "POST")
    @PostMapping(value = "/getProductEva")
    public APIObjResponse<BasePage<ProductEvaDTO>> getProductEva(@Validated @RequestBody GetProductEvaModel getProductEvaModel){
        IPage<ProductEvaDTO> iPage = this.productEvaBiz.getProductEva(getProductEvaModel);
        BasePage<ProductEvaDTO> basePage = IpageToPage.toBasePage(iPage, ProductEvaDTO.class);
        return APIObjResponse.success("查询成功", basePage);
    }

    /**
     * 修改商品排序值
     * @param updateProductSortModel
     * @return
     */
    @ApiOperation(value = "修改商品排序值", httpMethod = "POST")
    @PostMapping(value = "/updateProductSort")
    public APIObjResponse<Integer> updateProductSort(@Validated @RequestBody UpdateProductSortModel updateProductSortModel){
        UserInfo userInfo = super.getLoginUser();
        int result = this.cmsProductBiz.updateProductSort(updateProductSortModel, userInfo.getBaseUserId());
        return APIObjResponse.success("操作成功", result);
    }

}
